<?php
/**
 * Created by PhpStorm.
 * User: yangyulu
 * Date: 2020/3/10
 * Time: 上午10:44
 */
class Admin_questionnaireController extends Admin_baseController{
    public function listAction(){
        $page = Request::getParams('page', 1);
        $pageSize = Request::getParams('pageSize', 20);
        $categoryId=Request::getParams("categoryId");
        $where = $value = $url = array();
        if ($categoryId) {
            $where[] = 'project_id = ?';
            $value[] = $categoryId;
            $url['project_id'] = $categoryId;
        }
        $total = self::model('questionnaire')->count('id', $where, $value);
        $list = self::model('questionnaire')->limit(($page - 1) * $pageSize, $pageSize)->order('id desc')->getAll($where, $value);
        $this->assign(array(
            'page' => $page,
            'pageSize' => $pageSize,
            'categoryId' => $categoryId,
            'categoryList' => self::model('project_list')->getAll(),
            'total' => $total,
            'list' => $list,
            'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_questionnaire/list?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
        ));
        $this->display();
    }

    public function editAction(){
        $ids = Request::getParams('ids');
        $name=Request::getParams("name");
        $project_id=Request::getParams("project_id");
        if ($ids && !$info = self::model('questionnaire')->getOne('id=?', $ids)) {
            Response::showMsg('信息查询失败，请稍后重试');
        }
        if (Request::isGet()) {
            $this->assign([
                'info' => $info,
                'categoryList' => self::model('project_list')->getList(),
            ]);

            $this->display();
            exit;
        }

        if(empty($name)){
            Response::ajax(NORMAL_ERROR,'请填写问卷地址');
        }

        if(empty($project_id)){
            Response::ajax(NORMAL_ERROR,'请填选择所属项目');
        }
        $data['name']=$name;
        $data['project_id']=$project_id;
        $data["update_time"]=date("Y-m-d H:i",time());
        if($ids){
            self::model("questionnaire")->update($data,"id=?",$ids);
            Response::ajax(200, '修改成功');
        }else{
            $data["add_time"]=date("Y-m-d H:i",time());
            self::model("questionnaire")->insert($data);
            Response::ajax(200, '添加成功');
        }
    }

    public function applyAction(){
        $ids=Request::getParams('ids');
        $info= self::model('questionnaire')->getOne(["id=?","is_default=?"],[$ids,0]);
        if(empty($info)){
            Response::showMsg('信息不存在，无法前台显示');
        }
        $res=self::model('questionnaire')->update(["is_default"=>1],"id=?",$ids);
        if($res){
            self::model('questionnaire')->update(["is_default"=>0],["id!=?","project_id=?"],[$ids,$info["project_id"]]);
            Response::ajax(200, "前台显示成功");
        }else{
            Response::showMsg('前台显示失败');
        }

    }
}